// pages/gerenyemian/grym.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: false,
    
    items: [
      {
        icon: '../../images/user/user-order-2.png',
        text: '我的订单',
        path: '/pages/order/orderlist/orderlist'
        
      },
      {
        icon: '../../images/user/jie_dan-2.png',
        text: '我的任务',
        path: '/pages/workerorder/workerorder'
        
      },
      {
        icon: '../../images/user/user-address-2.png',
        text: '我的地址',
        path: '/pages/address/toaddress/toaddress'
      },         
      {
        icon: '../../images/user/user-massage-2.png',
        text: '消息',
        path: '/pages/message/message',
        isunread: false,
        unreadNum: 0
      },
      {
        icon: '../../images/user/worker-2.png',
        text: '我是师傅',
        path: '/pages/userindex/userindex'

      },
      {
        icon: '../../images/user/user-board-2.png',
        text: '留言',
        path: '/pages/board/board'
        
      },
      {
        icon: '../../images/user/talk-2.png',
        text: '发表',
        path: '/pages/myTalk/myTalk'

      },
      {
        icon: '../../images/user/user-about-2.png',
        text: '关于开发者',
        path: '/pages/about/about'
      }
      
    ],
    settings: [
      {
        icon: '../../images/user/user-board-2.png',
        text: '留言',
        path: '....'
      },
      {
        icon: '../../images/user/user-about-2.png',
        text: '关于我们',
        path: '/pages/about/index'
      },
    ]
  },

  getUserInfo: function (e) {
    var that=this
    console.log(e);
    if (e.detail.errMsg.indexOf("ok") != -1) {
      //this.setData({ userInfo: e.detail.userInfo });
      //wx.setStorageSync("userInfo", e.detail.userInfo);//将对象存入本地缓存
      wx.setStorageSync("city", e.detail.userInfo.city);//将地址存入本地缓存
      console.log("这是.....：" + e.detail.userInfo.nickName);
      wx.login({
        success:function(res){
          console.log("res...code..：" + res.code);
          //var code=res.code;
          wx.request({
            url: app.globalData.baseUrl +'/userinfo/login',
            method:"POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {//获取数据传到后台
              code: res.code,
              avatarUrl: e.detail.userInfo.avatarUrl,
              userName: e.detail.userInfo.nickName,
              userGender: e.detail.userInfo.gender,
              userCity: e.detail.userInfo.city,
              userProvince: e.detail.userInfo.province,
              userCountry: e.detail.userInfo.country,
            },

            success: function (res) {
              console.log("这是....：" + res.data.userInfo.buyerOpenid);
              var user = res.data.userInfo
              ///
              if (user == null) {
                var toastText = '获取数据失败' + res.data.errMsg;
                wx.showToast({
                  title: toastText,
                  icon: '',
                  duration: 2000
                });
                that.setData({ userInfo: null });
              } else {
                // 将返回的数据保存到全局的缓冲中，方便其他页面使用
                wx.setStorageSync("openid", user.buyerOpenid);//将对象存入本地缓存
                wx.setStorageSync("userInfo", user);//将对象存入本地缓存
                that.setData({ userInfo: user });

              }

              // 将返回的数据保存到全局的缓冲中，方便其他页面使用
              // wx.setStorageSync("openid", openid);//将对象存入本地缓存
            }
          })
        }
      })

    }
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this
    let userInfo = wx.getStorageSync("userInfo");
    //console.log("userInfo：", userInfo)
    //if (null != userInfo && "" != userInfo && userInfo != undefined) {
   /*if (app.globalData.isok == true) {
      that.setData({ userInfo: userInfo });
      console.log("app.globalData.isok：", app.globalData.isok)
      //that.getUnRead();
      
      
    }*/
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {         
          //app.globalData.isok = true;
          that.setData({ userInfo: userInfo });
          that.getUnRead()
        }
      }
    })


    //
    /*wx.setNavigationBarTitle({
      title: '我的',
    })
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: "scope.userInfo",
            success: (
              wx.getUserInfo({
                success: function (res) {
                  console.log(res.userInfo)
                }
              })
            )
          }
          )
        }
      }
    })*/
  },
  onShow: function (){
    var that = this
    let userInfo = wx.getStorageSync("userInfo");
    //console.log("userInfo：", userInfo)
    //if (null != userInfo && "" != userInfo&& userInfo!=undefined) {
    /*if (app.globalData.isok == true) {
      that.setData({ userInfo: userInfo });
      //that.getUnRead();
    }*/
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          //app.globalData.isok = true;
          that.setData({ userInfo: userInfo });
          that.getUnRead()
        }
      }
    })


    //////////查找未读信息
    //var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    var number =0;
    
    wx.request({
      url: app.globalData.baseUrl +'/board/getunread',
      method: 'GET',
      data: { "buyerOpenid": openid },//传入的参数
      success: function (res) {
        number = res.data.number;

        console.log("number", number)
        if (number == 0) {
         
         that.setData({
            'items[3].isunread': false,
            
          });
        } else {
            
          that.setData({
            'items[3].isunread': true,
            'items[3].unreadNum': number
          });       
        }
      }
    })
  },

  getUnRead:function(){
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    var number = 0;

    wx.request({
      url: app.globalData.baseUrl + '/board/getunread',
      method: 'GET',
      data: { "buyerOpenid": openid },//传入的参数
      success: function (res) {
        number = res.data.number;

        console.log("number", number)
        if (number == 0) {
          /* var toastText = '没有未读消息' + res.data.errMsg;
           wx.showToast({
             title: toastText,
             icon: '',
             duration: 2000
           });*/

          that.setData({
            'items[3].isunread': false,

          });
        } else {

          that.setData({
            'items[3].isunread': true,
            'items[3].unreadNum': number
          });
        }
      }
    })
  },

  navigateTo:function(e){
    let openid = wx.getStorageSync("openid");
    console.log("openid：", openid)
    var url=e.currentTarget.dataset.path;

    console.log('url:' + url);
    wx.navigateTo({
      url: url +'?openid='+ openid

  })
  }

  



})
